<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');



//--Counter ----Subha()--//
if (!function_exists('counter')) {

    function counter($table_name='', $status=51) {
        $ci = & get_instance();
        $ci->load->database();
        if ($table_name && $status) {
            $sql = "select count(*) as total from $table_name where status=$status";
            $query = $ci->db->query($sql);
            $row = $query->row();
            if ($row->total != 0)
                return $row->total;
            else
                return 0;
        } else {
            return 0;
        }
    }

}

//--Money Format----// 
if (!function_exists('moneyFormat')) {

    function moneyFormat($num) {
        $explrestunits = "";
        if (strlen($num) > 3) {
            $lastthree = substr($num, strlen($num) - 3, strlen($num));
            $restunits = substr($num, 0, strlen($num) - 3); // extracts the last three digits
            $restunits = (strlen($restunits) % 2 == 1) ? "0" . $restunits : $restunits; // explodes the remaining digits in 2's formats, adds a zero in the beginning to maintain the 2's grouping.
            $expunit = str_split($restunits, 2);
            for ($i = 0; $i < sizeof($expunit); $i++) {
                // creates each of the 2's group and adds a comma to the end
                if ($i == 0) {
                    $explrestunits .= (int) $expunit[$i] . ","; // if is first value , convert into integer
                } else {
                    $explrestunits .= $expunit[$i] . ",";
                }
            }
            $thecash = $explrestunits . $lastthree;
        } else {
            $thecash = $num;
        }
        return $thecash; // writes the final format where $currency is the currency symbol.
    }

}

//-- Random Text (Used for Password Generation//    
if (!function_exists('random_text')) {

    function random_text($lenght=Null) {
        $this->load->helper('string');
        return random_string('numeric', $lenght);
    }

}
//--Get Visitor IP --/
if (!function_exists('get_ip')) {

    function get_ip() {
        //--GET CLIENT IP    
        if (!empty($_SERVER['HTTP_CLIENT_IP'])) {   //check ip from share internet
            $ip = $_SERVER['HTTP_CLIENT_IP'];
        } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {   //to check ip is pass from proxy
            $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
        } else {
            $ip = $_SERVER['REMOTE_ADDR'];
        }
        return $ip;
    }

}